package com.lyx.crm.controller;

import com.lyx.crm.annotation.RequiredPermission;
import com.lyx.crm.base.BaseController;
import com.lyx.crm.base.ResultInfo;
import com.lyx.crm.query.RoleQuery;
import com.lyx.crm.service.RoleService;
import com.lyx.crm.utils.AssertUtil;
import com.lyx.crm.vo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.lyx.crm.controller
 * @ClassName: RoleController
 * @Author: lyx
 * @CreateTime: 2021/8/28 19:31
 * @Description:
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("queryAllRoles")
    @ResponseBody
    public List<Map<String, Object>> queryAllRoles(Integer userId) {
        return roleService.queryAllRolesByUserId(userId);
    }

    /**
     * 打开角色页面
     *
     * @return
     */
    @RequiredPermission(code = "6020")
    @RequestMapping("index")
    public String index() {
        return "role/role";
    }

    /**
     * 根据查询条件查询role表
     *
     * @param roleQuery
     * @return
     */
    @RequiredPermission(code = "602002")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryAllRoles(RoleQuery roleQuery) {

        return roleService.queryByParamsForTable(roleQuery);
    }

    /**
     * 打开添加/修改页面
     *
     * @param roleId
     * @param request
     * @return
     */
    @RequestMapping("toAddOrUpdateRolePage")
    public String toAddOrUpdateRolePage(Integer roleId, HttpServletRequest request) {
        if (null != roleId) {
            Role role = roleService.selectByPrimaryKey(roleId);
            AssertUtil.isTrue(null == role, "要编辑的角色不存在");
            request.setAttribute("role", role);
        }
        return "/role/add_update";

    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @RequiredPermission(code = "602001")
    @ResponseBody
    @RequestMapping("add")
    public ResultInfo addRole(Role role) {
        roleService.addRole(role);
        return success("添加角色成功");
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @RequiredPermission(code = "602003")
    @ResponseBody
    @RequestMapping("update")
    public ResultInfo updateRole(Role role) {
        roleService.updateRole(role);
        return success("修改角色成功");
    }

    /**
     * @param roleId
     * @return
     */

    @RequiredPermission(code = "602004")
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteRole(Integer roleId) {
        roleService.deleteRoleById(roleId);
        return success("删除角色成功");
    }

    /**
     * @param roleId
     * @param mIds
     * @return
     */
    @RequiredPermission(code = "602005")
    @RequestMapping("addGrant")
    @ResponseBody
    public ResultInfo addGrant(Integer roleId, Integer[] mIds) {

        roleService.addGrant(roleId, mIds);
        return success("添加资源成功");
    }
}
